#include "form_setting_data_tools.h"
#include "ui_form_setting_data_tools.h"

// add nacessary includes here
#include "utils/upgrade/upgrade_bll.h"
#include "ui/share/form_shield_dlg.h"
#include "ui/share/form_prompt_dlg.h"
#include "ui/share/form_inquiry_dlg.h"

/*
 * Function:    FormSettingDataTools
 * Description: The default constructor
 * Paramer:     void
 * Return:
 */
FormSettingDataTools::FormSettingDataTools(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FormSettingDataTools)
{
    qDebug() << Q_FUNC_INFO;

    ui->setupUi(this);
}

/*
 * FormHistoryResult:    ~FormSettingDataTools
 * Description: The default destructor
 * Paramer:     void
 * Return:
 */
FormSettingDataTools::~FormSettingDataTools()
{
    qDebug() << Q_FUNC_INFO;

    delete ui;
}

/*
 * Function:    getInstance
 * Description: Get an instance of "FormSettingDataTools" class, if exist, return it
 * Paramer:     QWidget *pParent - the pointer of QWidget class
 * Return:      the pointer of "FormSettingDataTools" class
 */
FormSettingDataTools* FormSettingDataTools::getInstance(QWidget *pParent)
{
    static FormSettingDataTools *pNew = NULL;
    if(!pNew) {
        qDebug() << Q_FUNC_INFO;

        // Create a new object if it doesn't exist
        pNew = new FormSettingDataTools(pParent);
        if(NULL == pNew) return NULL;

        // After constructing object, initialize the object
        pNew->_initialze();

        // Initialize control's UI
        pNew->_initUi();

        // Hide widget firstly
        pNew->hide();
    }
    else {
        // Set the parent object
        pNew->setParent(pParent);
    }

    return pNew;
}

/*
 * Function:    _initialze
 * Description: After constructing object, initialize the object
 * Paramer:     void
 * Return:      void
 */
void FormSettingDataTools::_initialze()
{
    qDebug() << Q_FUNC_INFO;

    // Signal-slot initialization
    std::vector<bool> vecConnect;
    for(int i=0; i<(int)vecConnect.size(); i++) {
        Q_ASSERT(vecConnect.at(i));
    }
}

/*
* Function:		_initUi
* Description:	Init the window style
* Paramer:		void
* Return:		void
*/
void FormSettingDataTools::_initUi()
{
    qDebug() << Q_FUNC_INFO;

    // Displays the background image of the window
    QWidget::setAttribute(Qt::WA_StyledBackground);

    SET_STYLE_SHEET(QString(":/%1/setting/%2.qss").arg("skin_1").arg(this->objectName()));

    ui->pushButton_Backup->setFocusPolicy(Qt::NoFocus);
    ui->pushButton_Restore->setFocusPolicy(Qt::NoFocus);
    ui->pushButton_RestoreFactory->setFocusPolicy(Qt::NoFocus);
}

/*
* Function:		changeEvent
* Description:	Triggers an event before it is switch language
* Paramer:		QShowEvent *event - the pointer of "QEvent"
* Return:		void
*/
void FormSettingDataTools::changeEvent(QEvent *e)
{
    switch (e->type()) {
    case QEvent::LanguageChange:
    {
        ui->retranslateUi(this);
        qDebug() << Q_FUNC_INFO;
        break;
    }
    default:
        break;
    }

    QWidget::changeEvent(e);
}

void FormSettingDataTools::on_pushButton_Backup_clicked()
{
    FormInquiryWidget::getInstance(this)->setInquiryInfo(tr("Are you sure to making data backup?"), E_WA_SET_DATA_BACKUP);
    // Wait for the slotRespondInfoFromDlg function to respond
}

void FormSettingDataTools::on_pushButton_Restore_clicked()
{
    FormInquiryWidget::getInstance(this)->setInquiryInfo(tr("Are you sure to making data restore?"), E_WA_SET_DATA_RESTORE);
    // Wait for the slotRespondInfoFromDlg function to respond
}

void FormSettingDataTools::on_pushButton_RestoreFactory_clicked()
{
    FormInquiryWidget::getInstance(this)->setInquiryInfo(tr("Restoring factory settings will clear all data and settings. Are you sure to execute?"), E_WA_SET_DATA_RESTORE_FACTORY);
    // Wait for the slotRespondInfoFromDlg function to respond
}

/*
* Function:		slotRespondInfoFromDlg
* Description:	Get the reply message from the child window
* Paramer:		int nReplyType - the reply type
*               bool bAccepted - is confirmed?
* Return:		void
*/
void FormSettingDataTools::slotRespondInfoFromDlg(int nReplyType, bool bAccepted)
{
    qDebug() << Q_FUNC_INFO << nReplyType << bAccepted;

    // Prevent the button from clicking repeatedly quickly
    FormShieldDlg::getInstance(this)->show();

    switch (nReplyType) {
    case E_WA_SET_DATA_BACKUP:
    {
        // Start data backup
        int nRetVal = UpgradeBLL::getInstance(this)->dataBackup(DEF_UDISK_BACKUP);
        switch (nRetVal) {
        case 0:
            FormPromptWidget::getInstance(this)->setPromptInfo(tr("Prompt"), tr("Data backup complete"), true);
            break;
        case -1:
            FormPromptWidget::getInstance(this)->setPromptInfo(tr("Prompt"), tr("No U disk was detected"));
            break;
        case -2:
            FormPromptWidget::getInstance(this)->setPromptInfo(tr("Prompt"), tr("The opt directory was not detected"));
            break;
        case -3:
            FormPromptWidget::getInstance(this)->setPromptInfo(tr("Prompt"), tr("The opt/backup directory was not detected"));
            break;
        case -4:
            FormPromptWidget::getInstance(this)->setPromptInfo(tr("Prompt"), tr("The file or directory is no exist"));
            break;
        case -5:
            FormPromptWidget::getInstance(this)->setPromptInfo(tr("Prompt"), tr("No files were found that need to be backed up"));
            break;
        default:
            FormPromptWidget::getInstance(this)->setPromptInfo(tr("Prompt"), tr("Data backup failed") + QString(" [%1]").arg(nRetVal), true);
            break;
        }

        break;
    }
    case E_WA_SET_DATA_RESTORE:
    {
        // Start data restore
        int nRetVal = UpgradeBLL::getInstance(this)->dataRestore(DEF_UDISK_BACKUP);
        switch (nRetVal) {
        case 0:
            FormPromptWidget::getInstance(this)->setPromptInfo(tr("Prompt"), tr("Data restore complete, the system will restart"), true);

#ifdef QT_NO_DEBUG
            QTimer::singleShot(1000, this, [=](){
                // Reboot system
                ::system(QString("/sbin/reboot").toLatin1().data());
            });
#else
            QTimer::singleShot(5000, this, [=](){
                QApplication::exit();
            });
#endif
            break;
        case -1:
            FormPromptWidget::getInstance(this)->setPromptInfo(tr("Prompt"), tr("The directory is no exist") + QString(" (%1)").arg(DEF_UDISK_BACKUP));
            break;
        case -2:
            FormPromptWidget::getInstance(this)->setPromptInfo(tr("Prompt"), tr("The backup file is no exist"));
            break;
        default:
            FormPromptWidget::getInstance(this)->setPromptInfo(tr("Prompt"), tr("Data restore failed") + QString(" [%1]").arg(nRetVal), true);
            break;
        }

        break;
    }
    case E_WA_SET_DATA_RESTORE_FACTORY:
    {
        // Start restore factory
        int nRetVal = UpgradeBLL::getInstance(this)->restoreFactory();
        if(0 == nRetVal) {
            FormPromptWidget::getInstance(this)->setPromptInfo(tr("Prompt"), tr("Execute successful, the system will restart"));

#ifdef QT_NO_DEBUG
            QTimer::singleShot(1000, this, [=](){
                // Reboot system
                ::system(QString("/sbin/reboot").toLatin1().data());
            });
#else
            QTimer::singleShot(5000, this, [=](){
                QApplication::exit();
            });
#endif
        }
        else {
            FormPromptWidget::getInstance(this)->setPromptInfo(tr("Prompt"), tr("Restore factory failed") + QString(" [%1]").arg(nRetVal));
        }
        break;
    }
    }

    FormShieldDlg::getInstance(this)->hide();
}
